package com.dkts.oil.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dkts.oil.config.OilConfig;
import com.dkts.oil.converter.GeometryConverter;
import com.dkts.oil.converter.WriterToFile;
import com.dkts.oil.exception.OilException;
import com.dkts.oil.model.GHPoint;
import com.dkts.oil.model.ObstacleModel;
import com.dkts.oil.web.vo.request.TransferStationRequest;
import com.dkts.oil.web.vo.request.subReq.ObstacleType;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Polygon;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 判断事铁路还是水路
 */
public class CheckSubWayOrWaterway {

    @Autowired
    public OilConfig oilConfig;

//    /**
//     * 判断是否走水路
//     * @param distance
//     * @return
//     */
//    public boolean isWaterway(TransferStationRequest distance) {
//
//        return false;
//    }

    public String getRoadType(TransferStationRequest request) {

        return oilConfig.getRailwayUrl();
    }

    public JSONObject defaultRouteParam(TransferStationRequest request) {
        if(request == null){
            throw new OilException("请求参数不能为空！");
        }
        List points=request.getPoints();
        if(points == null || points.size() < 2){
            throw new OilException("路线规划至少需要两个坐标点！");
        }


        String parms="{\"profile\":\"car\",\"elevation\":true,\"instructions\":true,\"locale\":\"zh_CN\",\"points_encoded\":false,\"points_encoded_multiplier\":1000000,\"details\":[\"road_class\",\"road_environment\",\"max_speed\",\"average_speed\"],\"snap_preventions\":[\"ferry\"]}";
        JSONObject jsonObject = JSONObject.parseObject(parms);
        jsonObject.put("points",request.getPoints());
        if (request.getPathsNum() != null){
            jsonObject.put("alternative_route.max_paths",request.getPathsNum());
        }
        jsonObject.put("obstacle_vos",getDefaultObs(request.getObstacle()));
        return jsonObject;
    }

    public List<ObstacleModel> getDefaultObs(List<ObstacleType> obstacle){
        List<ObstacleModel> listObstacles = new ArrayList<>();
        if (obstacle == null || obstacle.size() ==0){
            return listObstacles;
        }
        try  {
            for (int i=0;i<obstacle.size();i++) {
                ObstacleType obstacleType = obstacle.get(i);
                Geometry geometry = GeometryConverter.convert(obstacleType.getBoundWkt());
                if(obstacleType.getBufferWidth() != null && obstacleType.getBufferWidth() !=0){
                    Geometry geometry2= GeometryConverter.getBuff(geometry, obstacleType.getBufferWidth());
                    listObstacles.add(ObstacleModel.create("a"+i, 0, gpsPointStr((Polygon) geometry2)));
                    continue;
                }
                listObstacles.add(ObstacleModel.create("a"+i, 0,gpsPointStr((Polygon) geometry)));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new OilException("障碍物参数错误！");
        }
        return listObstacles;
    }

    private String gpsPointStr(Polygon polygon){
        Coordinate[] coordinates = polygon.getCoordinates();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < coordinates.length; i++) {
            Coordinate coordinate = coordinates[i];
            sb.append(coordinate.x).append(",").append(coordinate.y);
            if (i != coordinates.length - 1) {
                sb.append(";");
            }
        }
        return sb.deleteCharAt(sb.length() - 1).toString();
    }

}
